#include "../include/BloomFilter.h"

void Test()
{
    bitset::BloomFilter<20> bloom;
    bloom.set("猪八戒");
    bloom.set("沙和尚");
    bloom.set("唐僧");

    // bitset::defaultErrorClass.printMessage(bloom.check("猪八戒"));
    bitset::PrintError<bool>().printMessage(bloom.check("猪八戒"));
    bitset::PrintError<bool>().printMessage(bloom.check("沙和尚"));
    bitset::PrintError<bool>().printMessage(bloom.check("唐僧"));
}

void TestBloomFilter1()
{
    bitset::BloomFilter<10> bf;
    bf.set("猪八戒");
    bf.set("孙悟空");
    bf.set("唐僧");

    std::cout << bf.check("猪八戒") << std::endl;
    std::cout << bf.check("孙悟空") << std::endl;
    std::cout << bf.check("唐僧") << std::endl;
    std::cout << bf.check("沙僧") << std::endl;
    std::cout << bf.check("猪八戒1") << std::endl;
    std::cout << bf.check("猪戒八") << std::endl;
}

void TestBloomFilter2()
{
    // srand(time(0));
    const size_t N = 1000000;
    // bitset::BloomFilter<N> bf;
    // bitset::BloomFilter<N, 3> bf;
    bitset::BloomFilter<N, 10> bf;

    std::vector<std::string> v1;
    // std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html";
    // std::string url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=65081411_1_oem_dg&wd=ln2&fenlei=256&rsv_pq=0x8d9962630072789f&rsv_t=ceda1rulSdBxDLjBdX4484KaopD%2BzBFgV1uZn4271RV0PonRFJm0i5xAJ%2FDo&rqlang=en&rsv_enter=1&rsv_dl=ib&rsv_sug3=3&rsv_sug1=2&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=330&rsv_sug4=2535";
    std::string url = "猪八戒";

    for (size_t i = 0; i < N; ++i)
    {
        v1.push_back(url + std::to_string(i));
    }

    for (auto &str : v1)
    {
        bf.set(str);
    }

    // v2跟v1是相似字符串集（前缀一样），但是后缀不一样
    v1.clear();
    for (size_t i = 0; i < N; ++i)
    {
        std::string urlstr = url;
        urlstr += std::to_string(9999999 + i);
        v1.push_back(urlstr);
    }

    size_t n2 = 0;
    for (auto &str : v1)
    {
        if (bf.check(str)) // 误判
        {
            ++n2;
        }
    }
    std::cout << "相似字符串误判率:" << (double)n2 / (double)N << std::endl;

    // 不相似字符串集  前缀后缀都不一样
    v1.clear();
    for (size_t i = 0; i < N; ++i)
    {
        // string url = "zhihu.com";
        std::string url = "孙悟空";
        url += std::to_string(i + rand());
        v1.push_back(url);
    }

    size_t n3 = 0;
    for (auto &str : v1)
    {
        if (bf.check(str))
        {
            ++n3;
        }
    }
    std::cout << "不相似字符串误判率:" << (double)n3 / (double)N << std::endl;

    std::cout << "公式计算出的误判率:" << bf.getFalseProbability() << std::endl;
}
int main()
{
    Test();
    // TestBloomFilter1();
    TestBloomFilter2();

    return 0;
}